﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace Alfa.Common.ValidationRules {
	public class DateValidationRule : ValidationRuleBase {
		public DateValidationRule( ) { }

		public bool Nullable { get; set; }

		public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
			if (value is DateTime || (this.Nullable && value.Equals(null))) { // Does value is already DateTime?

			} else if (value == null) {
				if (!this.Nullable)
					return this.ParseError(false, Messages.NotADate);
			} else if (value is string) { // Does value is string?
				if (((string)value).Length > 0) {
					DateTime parsedDateTime;
					if (!DateTime.TryParse((string)value, out parsedDateTime))
						return this.ParseError(false, Messages.NotADate);
				} else if (!this.Nullable)
					return this.ParseError(false, Messages.NotADate);
			} else {
				throw new ArgumentException(this.GetType( ).ToString( ) + " validator can not validate provided value (Type " + (value != null ? value.GetType( ).ToString( ) : "null") + " is not supported)!");
			}
			return this.ParseError(true, null);
		}
	}
}
